package com.study.servlet.junit;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

import org.junit.Test;
import org.omg.PortableServer.POA;

/**
* @Name: ReadResourceFileTest
* @Description: 读取资源配置文件测试类
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-1 上午9:59:29 
* @Version: V1.0
 */
public class ReadResourceFileTest {
	
	/**
	* @Name: testResourceBundle
	* @Description: 使用ResourceBundle读取资源属性文件
	* 特点：
		使用ResourceBundle类，只能加载类路径（即src目录下）目录下的文件，且只能读取属性配置文件
		资源文件路径使用基名表示：包名.资源文件名称（不带扩展名）或包名/资源名称（不带扩展名）
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 上午10:16:07
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void testResourceBundle() {
		ResourceBundle bundle = ResourceBundle.getBundle("com.study.servlet.servletcontext.db") ;
//		ResourceBundle bundle = ResourceBundle.getBundle("com/study/servlet/servletcontext/db") ;
		Enumeration<String> keys = bundle.getKeys() ;
		while(keys.hasMoreElements()) {
			String key = keys.nextElement() ;
			String value = bundle.getString(key) ;
			System.out.println(key + ":" + value);
		}
	}
	
	/**
	* @Name: testClassLoader
	* @Description: 使用ClassLoader读取资源配置文件
	* 特点：
		只能加载类路径下（classes目录下）的文件
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 上午10:17:31
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void testClassLoader() {
		ClassLoader loader = ReadResourceFileTest.class.getClassLoader() ;
		InputStream is = loader.getResourceAsStream("db.properties") ;
//		InputStream is = loader.getResourceAsStream("com/study/servlet/servletcontext/db.properties") ;	
		Properties props = new Properties() ;
		try {
			props.load(is) ;
			Enumeration<String> propertyNames = (Enumeration<String>) props.propertyNames() ;
			while(propertyNames.hasMoreElements()) {
				String propertyName = propertyNames.nextElement() ;
				String propertyValue = props.getProperty(propertyName) ;
				System.out.println(propertyName + ":" + propertyValue);
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	/**
	* @Name: testURL
	* @Description: 使用URL对象获取资源文件绝对路径
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 上午10:33:07
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void testURL() {
		ClassLoader loader = ReadResourceFileTest.class.getClassLoader() ;
//		URL url = loader.getResource("db.properties") ;
		URL url = loader.getResource("com/study/servlet/servletcontext/db.properties") ;	
		String path = url.getPath() ;
		System.out.println(path);
		Properties props = new Properties() ;
		try {
			props.load(new FileInputStream(path)) ;
			Enumeration<String> keys = (Enumeration<String>) props.propertyNames() ;
			while(keys.hasMoreElements()) {
				String key = keys.nextElement() ;
				String value = props.getProperty(key) ;
				System.out.println(key + ":" + value);
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	/**
	* @Name: testURLEnDecode
	* @Description: URL编码解码测试
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 上午10:41:36
	* @Parameters: 
	* @Return: void
	 */
	@Test
	public void testURLEnDecode() {
		String name = "张三" ;
		try {
			//将指定字符串内容按照指定编码格式进行编码
			String encode = URLEncoder.encode(name, "UTF-8") ;
			System.out.println(encode);
			//解码
			String decode = URLDecoder.decode(encode, "UTF-8") ;
			System.out.println(decode);
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	

}
